package com.dianrui.moonfire.activity;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.adapter.IncomeDetailsAdapter;
import com.dianrui.moonfire.bean.InvestBean;
import com.dianrui.moonfire.bean.InvestTop;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:收益明细
 * User: luoziyin
 * Date: 2020-8-17
 */
public class IncomeDetailsListAcitivity extends BaseActivity {
    private IncomeDetailsAdapter incomeDetailsAdapter;
    private List<InvestTop> investBeanList = new ArrayList<>();
    private List<InvestBean> investBeanLists = new ArrayList<>();

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.title)
    TextView title;


    @Override
    public int getContentViewId() {
        return R.layout.activity_incomedetails_list;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText("收益明细");
        getInvest();
        incomeDetailsAdapter = new IncomeDetailsAdapter(this,investBeanList);
        listview.setAdapter(incomeDetailsAdapter);
    }

    @Override
    public void initData() {}


    private void getInvest() {
        XHttpRequest.getInstance().postWithMemberToken(Url.CONTRACT_INVEST, "",  new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        InvestTop investTop = new InvestTop();
                                        JSONObject jsonObject1 = jsonArray.optJSONObject(i);
                                        investTop.createTime = jsonObject1.optString("create_time");
                                        investTop.total = jsonObject1.optString("total");
                                        JSONArray jsonArray1 = jsonObject1.optJSONArray("subclass");
                                        if (jsonArray1.length() > 0) {
                                            for (int j = 0; j < jsonArray1.length(); j++) {
                                                InvestBean investBean = new InvestBean();
                                                investBean.invest_id = jsonArray1.optJSONObject(j).optString("invest_id");
                                                investBean.years = jsonArray1.optJSONObject(j).optString("years");
                                                investBean.money = jsonArray1.optJSONObject(j).optString("money");
                                                investBean.create_time = jsonArray1.optJSONObject(j).optString("create_time");
                                                investBean.order_code = jsonArray1.optJSONObject(j).optString("order_code");
                                                investBean.number = jsonArray1.optJSONObject(j).optString("number");
                                                investBeanLists.add(investBean);
                                            }
                                            investTop.list = investBeanLists;
                                            investBeanList.add(investTop);

                                            if (incomeDetailsAdapter != null) {
                                                incomeDetailsAdapter.refresh(investBeanList);
                                            }
                                        }

                                    }
                                }
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                SPUtils.getInstance().clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpKillActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });


    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;

        }
    }
}
